~ chicken-core (chicken-5) /manual/Module (chicken memory representation)


  1[[tags: manual]]
  2[[toc:]]
  3
  4== Module (chicken memory representation)
  5
  6The procedures from this module operate on the in-memory
  7representation of Scheme objects.  These procedures ''are'' safe, so,
  8for example, unlike the procedures from {{(chicken memory)}} these
  9procedures ''will'' type-check and range-check their arguments, but
 10you still need to know what you're doing because the effects may be
 11surprising for the uninitiated.
 12
 13
 14=== Extending procedures with data
 15
 16
 17==== extend-procedure
 18
 19<procedure>(extend-procedure PROCEDURE X)</procedure>
 20
 21Returns a copy of the procedure {{PROCEDURE}} which contains an
 22additional data slot initialized to {{X}}. If {{PROCEDURE}} is already
 23an extended procedure, then its data slot is changed to contain {{X}}
 24and the same procedure is returned. Signals an error when
 25{{PROCEDURE}} is not a procedure.
 26
 27
 28==== extended-procedure?
 29
 30<procedure>(extended-procedure? PROCEDURE)</procedure>
 31
 32Returns {{#t}} if {{PROCEDURE}} is an extended procedure,
 33or {{#f}} otherwise.
 34
 35
 36==== procedure-data
 37
 38<procedure>(procedure-data PROCEDURE)</procedure>
 39
 40Returns the data object contained in the extended procedure
 41{{PROCEDURE}}, or {{#f}} if it is not an extended procedure.
 42
 43
 44==== set-procedure-data!
 45
 46<procedure>(set-procedure-data! PROCEDURE X)</procedure>
 47
 48Changes the data object contained in the extended procedure
 49{{PROCEDURE}} to {{X}}. Signals an error when {{PROCEDURE}} is not an
 50extended procedure.
 51
 52<enscript highlight=scheme>
 53(define foo
 54  (letrec ((f (lambda () (procedure-data x)))
 55           (x #f) )
 56    (set! x (extend-procedure f 123))
 57    x) )
 58(foo)                                         ==> 123
 59(set-procedure-data! foo 'hello)
 60(foo)                                         ==> hello
 61</enscript>
 62
 63
 64
 65=== Low-level data access
 66
 67These procedures operate with what are known as ''vector-like
 68objects''. A ''vector-like object'' is a vector, record structure,
 69pair, symbol or keyword: it is an aggregation of other Scheme objects.
 70
 71Note that strings and blobs are not considered vector-like (they are
 72considered to be ''byte vectors'', which are objects of mostly
 73unstructured binary data).
 74
 75
 76==== vector-like?
 77
 78<procedure>(vector-like? X)</procedure>
 79
 80Returns {{#t}} when {{X}} is a vector-like object, returns {{#f}}
 81otherwise.
 82
 83
 84==== block-ref
 85
 86<procedure>(block-ref VECTOR* INDEX)</procedure>
 87
 88Returns the contents of the {{INDEX}}th slot of the vector-like object
 89{{VECTOR*}}.
 90
 91
 92==== block-set!
 93
 94<procedure>(block-set! VECTOR* INDEX X)</procedure><br>
 95<procedure>(set! (block-ref VECTOR* INDEX) X)</procedure>
 96
 97Sets the contents of the {{INDEX}}th slot of the vector-like object
 98{{VECTOR*}} to the value of {{X}}.
 99
100==== number-of-slots
101
102<procedure>(number-of-slots VECTOR*)</procedure>
103
104Returns the number of slots that the vector-like object {{VECTOR*}}
105contains.
106
107
108==== number-of-bytes
109
110<procedure>(number-of-bytes BLOCK)</procedure>
111
112Returns the number of bytes that the object {{BLOCK}}
113contains. {{BLOCK}} may be any non-immediate value.
114
115
116==== object-copy
117
118<procedure>(object-copy X)</procedure>
119
120Copies {{X}} recursively and returns the fresh copy. Objects allocated
121in static memory are copied back into garbage collected storage.
122
123
124=== Record instance
125
126
127==== make-record-instance
128
129<procedure>(make-record-instance SYMBOL ARG1 ...)</procedure>
130
131Returns a new instance of the record type {{SYMBOL}}, with its
132slots initialized to {{ARG1 ...}}.  To illustrate:
133
134<enscript highlight=scheme>
135(define-record-type point (make-point x y) point?
136  (x point-x point-x-set!)
137  (y point-y point-y-set!))
138</enscript>
139
140expands into something quite similar to:
141
142<enscript highlight=scheme>
143(begin
144  (define (make-point x y)
145    (make-record-instance 'point x y) )
146  (define (point? x)
147    (and (record-instance? x)
148         (eq? 'point (block-ref x 0)) ) )
149  (define (point-x p) (block-ref p 1))
150  (define (point-x-set! p x) (block-set! p 1 x))
151  (define (point-y p) (block-ref p 2))
152  (define (point-y-set! p y) (block-set! p 1 y)) )
153</enscript>
154
155
156==== record-instance?
157
158<procedure>(record-instance? X [SYMBOL])</procedure>
159
160Returns {{#t}} if {{X}} is a record structure, or {{#f}} otherwise.
161
162Further, returns {{#t}} if {{X}} is of type {{SYMBOL}}, or {{#f}}
163otherwise.
164
165
166==== record-instance-type
167
168<procedure>(record-instance-type RECORD)</procedure>
169
170Returns type symbol of the record structure {{RECORD}}. Signals an
171error if {{RECORD}} is not a record structure.
172
173
174==== record-instance-length
175
176<procedure>(record-instance-length RECORD)</procedure>
177
178Returns number of slots for the record structure {{RECORD}}. The
179record-instance type is not counted. Signals an error if
180{{RECORD}} is not a record structure.
181
182
183==== record-instance-slot
184
185<procedure>(record-instance-slot RECORD INDEX)</procedure>
186
187Returns the contents of the {{INDEX}}th slot of the record structure
188{{RECORD}}. The slot index range is the open interval {{[0
189record-instance-length)}}. Signals an error if {{RECORD}} is not a record
190structure.
191
192
193==== record-instance-slot-set!
194
195<procedure>(record-instance-slot-set! RECORD INDEX X)</procedure><br>
196<procedure>(set! (record-instance-slot RECORD INDEX) X)</procedure>
197
198Sets the {{INDEX}}th slot of the record structure {{RECORD}} to
199{{X}}. The slot index range is the open interval {{[0
200record-instance-length)}}. Signals an error if {{RECORD}} is not a
201record structure.
202
203
204==== record->vector
205
206<procedure>(record->vector RECORD)</procedure>
207
208Returns a new vector with the type and the elements of the record
209structure {{RECORD}}. Signals an error if {{RECORD}} is not a record
210structure.
211
212
213=== Magic
214
215
216==== object-become!
217
218<procedure>(object-become! ALIST)</procedure>
219
220Changes the identity of the value of the car of each pair in {{ALIST}}
221to the value of the cdr. Neither value may be immediate (i.e. exact
222integers, characters, booleans or the empty list).
223
224<enscript highlight=scheme>
225(define x "i used to be a string")
226(define y '#(and now i am a vector))
227(object-become! (list (cons x y)))
228x                                    ==> #(and now i am a vector)
229y                                    ==> #(and now i am a vector)
230(eq? x y)                            ==> #t
231</enscript>
232
233Note: this operation invokes a major garbage collection.
234
235The effect of using {{object-become!}} on evicted data (see
236{{object-evict}}) is undefined.
237
238
239==== mutate-procedure!
240
241<procedure>(mutate-procedure! OLD PROC)</procedure>
242
243Replaces the procedure {{OLD}} with the result of calling the
244one-argument procedure {{PROC}}. {{PROC}} will receive a copy of
245{{OLD}} that will be identical in behaviour to the result of {{OLD}}:
246
247<enscript highlight=scheme>
248 ;;; Replace arbitrary procedure with tracing one:
249
250 (mutate-procedure! my-proc
251   (lambda (new)
252     (lambda args
253       (printf "~s called with arguments: ~s~%" new args)
254       (apply new args) ) ) )
255</enscript>
256
257
258---
259Previous: [[Module (chicken memory)]]
260
261Next: [[Module (chicken module)]]
Trap